import SettingsData from "../data/SettingsData"
import { TourTransitionType } from "../enum/tourTransition.enum"
import funcUtil from "../utils/func.util"
import TourTransitionFactory from "./TourTransitionFactory"

export default class TourDelayTransition {
  settingsData: SettingsData
  type: number
  toIndex: number
  currentTransitionPromise: any
  cancelDelay: () => any
  constructor(e) {
    this.settingsData = e
    this.type = TourTransitionType.Delay
    this.toIndex = -1
    this.currentTransitionPromise = null
    this.cancelDelay = () => null
  }
  get active() {
    return null !== this.currentTransitionPromise
  }
  get promise() {
    return this.currentTransitionPromise ? this.currentTransitionPromise : Promise.resolve()
  }
  async stop() {
    this.currentTransitionPromise && (this.cancelDelay(), (this.currentTransitionPromise = null))
  }
  start() {
    if (this.active) throw Error("Transition already active")
    const e = funcUtil.waitRunX(TourTransitionFactory.getDelayDuration(this.settingsData))
    this.cancelDelay = () => {
      e.cancel()
    }
    this.currentTransitionPromise = e.promise.then(() => this.stop())
    return this
  }
}
